#include "GameTimer.h"
#include <assert.h>
#include <iostream>

GameTimer::GameTimer(void (*calcMethod)(), void (*drawMethod)(), int fps) : _calculationMethod(calcMethod), _drawMethod(drawMethod),
	_enabled(false), _standard(0), _freq(0), _mspf(1000 / fps)

{
	LARGE_INTEGER l;
	QueryPerformanceFrequency(&l);
	_freq = l.QuadPart;
}


GameTimer::~GameTimer(void)
{
}

void GameTimer::Start() {
	_enabled = true;
	LARGE_INTEGER l;
	QueryPerformanceCounter(&l);
	_standard = l.QuadPart;
}

void GameTimer::Stop() {
	 _enabled = false; 
}

bool GameTimer::Check() {
	LARGE_INTEGER l;
	QueryPerformanceCounter(&l);
	LONGLONG now = l.QuadPart;
	LONGLONG ellapsed = (now - _standard) * 1000 / _freq;
	//std::cout << "Check(Ellapsed:" << ellapsed << ")" << std::endl;
	if (ellapsed > _mspf) {
		int calcNum = (int)(ellapsed / _mspf);
		if (_calculationMethod != NULL) {
			for (int i = 0; i < calcNum; i++) {
				_calculationMethod();
			}
		}
		_standard += (_mspf * calcNum) * _freq / 1000;
		if (_drawMethod != NULL) { _drawMethod(); }
		return true;
	}
	return false;
}
